package com.cremamobile.filemanager.file;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.cremamobile.filemanager.R;

public class DirFileEntry extends FileListEntry {
	private int childFileNumber;
	private List<FileListEntry> child;
	private boolean childIsExistButNotInsertChild = false;
	private Date lastModified;
	private long size;
	
	public DirFileEntry(File file) {
		super(file);
		lastModified = new Date(this.path.lastModified());
		calcChildsAndSize();
	}

	@Override
	public boolean isDir() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isDevice() {
		// TODO Auto-generated method stub
		return false;
	}

	public int getChildFileNumber() {
		return childFileNumber;
	}
	public List<FileListEntry> getChildLists() {
		return this.child;
	}
	public void setChildLists(List<FileListEntry> list) {
		this.childIsExistButNotInsertChild = false;
		this.child = list;
		this.childFileNumber = this.child != null ? this.child.size() : 0;
	}

	public void setChildNumber(int number) {
		this.childFileNumber = number;
		this.childIsExistButNotInsertChild = false;
	}
	
	public void setChildIsExistButNotInsertChild(boolean search) {
		this.childIsExistButNotInsertChild	 = search;
	}
	
	public boolean isNeedSearchChild() {
		return this.childIsExistButNotInsertChild;
	}
	
	public Date getLastModified() {
		return lastModified;
	}
	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}
	
	public long getSize() {
		if (this.size == 0) {
			return (this.size = calcSize());
		}
		return this.size;
	}
	
	private void calcChildsAndSize() {
		if (this.child != null) {
			int sum = 0;
			for (FileListEntry entry : this.child) {
				sum += entry.getSize();
			}
			this.size = sum;
		} else {
			File[] child = this.path.listFiles();
			if (child != null && child.length > 0) {
				int num = 0, sumSize = 0;
				this.childIsExistButNotInsertChild = true;
				for (File item : child) {
					if (item.isHidden())	continue;
					num++;
					sumSize += item.length();
				}
				this.childFileNumber = num;
				this.size = sumSize;
			} else {
				this.childIsExistButNotInsertChild = false;
				this.childFileNumber = 0;
				this.size = 0;
			}
		}
	}
	
	private long calcSize() {
		int sum = 0;
		if (this.child != null) {
			for (FileListEntry item : child) {
				sum += item.getSize();
			}
			return sum;
		}
		
		if (this.childIsExistButNotInsertChild) {
			File[] childs = this.path.listFiles();
			if (childs == null || childs.length <= 0) {
				this.childIsExistButNotInsertChild = false;
				this.childFileNumber = 0;
				return 0;
			}
			
			for (File item : childs) {
				if (item.isHidden()) continue;
				sum += item.length();
			}
			return sum;
		}
		return 0;
	}
	
	public void setSize(long size) {
		this.size = size;
	}

	public String getExt() {
		return "";
	}

	public String getExtForIcon() {
		return "dir";
	}

	@Override
	public String toString() {
		return "DirFileEntry [file:"+this.path
				+", name:"+this.name
				+",childFileNumber:" + this.childFileNumber
				+",childIsExistButNotInsertChild:"+this.childIsExistButNotInsertChild
				+", size:"+this.size
				+", lastModified:"+this.lastModified;
	}

}
